/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
 #include "jpeg_read.h"

/* gcc -c -O2 -Wall -o jpeg_read.o jpeg_read.c  */
void init_source (j_decompress_ptr cinfo)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;
  src->start_of_file = TRUE;
}


int fill_input_buffer (j_decompress_ptr cinfo)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;
  size_t nbytes;

  nbytes = INPUT_BUF_SIZE;

  src->pub.next_input_byte = src->buffer;
  src->pub.bytes_in_buffer = nbytes;
  src->start_of_file = FALSE;

  return TRUE;
}


void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
  my_src_ptr src = (my_src_ptr) cinfo->src;

  if (num_bytes > 0) {
    while (num_bytes > (long) src->pub.bytes_in_buffer) {
      num_bytes -= (long) src->pub.bytes_in_buffer;
      (void) fill_input_buffer(cinfo);
    }
    src->pub.next_input_byte += (size_t) num_bytes;
    src->pub.bytes_in_buffer -= (size_t) num_bytes;
  }
}

void term_source (j_decompress_ptr cinfo)
{
  /* no work necessary here */
}



void jpeg_mem_src (j_decompress_ptr cinfo, unsigned char *image)
{
  my_src_ptr src;

  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
    cinfo->src = (struct jpeg_source_mgr *)
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				  sizeof(my_source_mgr));
    src = (my_src_ptr) cinfo->src;
    src->buffer = (JOCTET *)image;
  }

  src = (my_src_ptr) cinfo->src;
  src->pub.init_source = init_source;
  src->pub.fill_input_buffer = fill_input_buffer;
  src->pub.skip_input_data = skip_input_data;
  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  src->pub.term_source = term_source;
  src->infile = NULL;
  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  src->pub.next_input_byte = NULL; /* until buffer loaded */
}

int read_jpg(unsigned char *data, char *filename,int *bytesread)
{
  FILE* fd;
  int num;
  printf("here1 \n");
  fd=fopen(filename,"r");
  printf("%s\n",filename);
  if(fd<=0)
  {	
	perror("read_jpg error: could not open file\n");
	exit(1);
  }
  printf("here2 \n");
  num=fread(data,sizeof(char),300000,fd);
  printf("here3 \n");
  fclose(fd);
  (*bytesread)=num;
  if(num>0)
	  return 1;
  else
	  return 0;	
}
  
void jpeg_read(unsigned char *image,char* filename, int *width, int *height)
{
    JSAMPROW buffer;		/* Output row buffer */
    int row_stride;		/* physical row width in output buffer */
    int len;
    struct jpeg_error_mgr jerr;
    
    cinfo.err = jpeg_std_error(&jerr);
    
    read_jpg(jpeg,filename,&len);
    jpeg_create_decompress(&cinfo);

    jpeg_mem_src(&cinfo, jpeg);
    jpeg_read_header(&cinfo, TRUE);
    cinfo.out_color_space=JCS_GRAYSCALE;
    jpeg_start_decompress(&cinfo);	
    (*width)=cinfo.output_width;
    (*height)=cinfo.output_height;
    printf("h:%d w:%d c:%d\n",cinfo.output_width,cinfo.output_height,cinfo.out_color_space);
    row_stride = cinfo.output_width;/* * cinfo.output_components;*/
    while (cinfo.output_scanline < cinfo.output_height) {
	buffer=(JSAMPROW )(image+cinfo.output_scanline*row_stride);
	jpeg_read_scanlines(&cinfo, &buffer, 1);
    }
    jpeg_finish_decompress(&cinfo);
}
void jpeg_read_i(char* filename, int *width, int *height)
{
    int len;
    struct jpeg_error_mgr jerr;
    
    cinfo.err = jpeg_std_error(&jerr);
    
    read_jpg(jpeg,filename,&len);
    jpeg_create_decompress(&cinfo);

    jpeg_mem_src(&cinfo, jpeg);
    jpeg_read_header(&cinfo, TRUE);
    cinfo.out_color_space=JCS_GRAYSCALE;
    jpeg_start_decompress(&cinfo);	
    (*width)=cinfo.output_width;
    (*height)=cinfo.output_height;
  //  jpeg_finish_decompress(&cinfo);
   
}
